package com.jxnu.duewang.zmly;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import com.jxnu.duewang.zmly.zmly.fragment.DiscoverFragment;
import com.jxnu.duewang.zmly.zmly.fragment.DownloadFragment;
import com.jxnu.duewang.zmly.zmly.fragment.MyFragment;
import com.jxnu.duewang.zmly.zmly.fragment.PlayFragment;
import com.jxnu.duewang.zmly.zmly.fragment.SubscribeFragment;
import com.jxnu.duewang.zmly.zouZhen.widget.DefineDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private FragmentManager mManager;
    private DiscoverFragment mDiscoverFragment;
    private SubscribeFragment mSubscribeFragment;
    private PlayFragment mPlayFragment;
    private DownloadFragment mDownloadFragment;
    private MyFragment mMyFragment;
    private long previousTime;

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mManager = getSupportFragmentManager();
        Fragment f = mManager.findFragmentByTag("discover");
        if (f != null) {
            mDiscoverFragment=(DiscoverFragment) f;
        }else {
            mDiscoverFragment = new DiscoverFragment();
        }
        f = mManager.findFragmentByTag("subscribe");
        if (f != null) {
            mSubscribeFragment=(SubscribeFragment) f;
        }else{
            mSubscribeFragment=new SubscribeFragment();
        }
        f = mManager.findFragmentByTag("play");
        if (f != null) {
            mPlayFragment=(PlayFragment)f;
        }else{
            mPlayFragment=new PlayFragment();
        }
        f = mManager.findFragmentByTag("download");
        if (f != null) {
            mDownloadFragment=(DownloadFragment) f;
        }else{
            mDownloadFragment=new DownloadFragment();
        }
        f = mManager.findFragmentByTag("my");
        if (f != null) {
            mMyFragment=(MyFragment) f;
        }else{
            mMyFragment=new MyFragment();
        }
        RadioGroup group = (RadioGroup) findViewById(R.id.main_tab_bar);
        if (group != null) {
            group.setOnCheckedChangeListener(this);
            group.check(R.id.main_tab_item_home);
        }

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction fragmentTransaction = mManager.beginTransaction();
        switch (checkedId) {
            case R.id.main_tab_item_home:
                fragmentTransaction.replace(R.id.fragment_container,mDiscoverFragment,"discover");
                break;
            case R.id.main_tab_item_search:
                fragmentTransaction.replace(R.id.fragment_container,mSubscribeFragment,"subscribe");
                break;
            case R.id.main_tab_item_community:
                fragmentTransaction.replace(R.id.fragment_container,mPlayFragment,"play");
                break;
            case R.id.main_tab_item_shopping_car:
                fragmentTransaction.replace(R.id.fragment_container,mDownloadFragment,"download");
                break;
            case R.id.main_tab_item_my:
                fragmentTransaction.replace(R.id.fragment_container,mMyFragment,"my");
                break;
        }
        fragmentTransaction.commit();
    }

    @Override
    public void onBackPressed() {
//        if(System.currentTimeMillis()-previousTime>3000){
//            previousTime = System.currentTimeMillis();
////            Toast.makeText(this, "在点击一次后退出", Toast.LENGTH_SHORT).show();
//        }else{
//            SharedPreferences message = getSharedPreferences("UserMessage", MODE_PRIVATE);
//            if (message != null) {
//                String string = message.getString("UserState", "未登录");
//                if ("已登录".equals(string)) {
//                    SharedPreferences.Editor edit = message.edit();
//                    edit.putString("UserState", "未登录");
//                    edit.apply();
//                }
//            }
//            super.onBackPressed();
//        }
        DefineDialog dialog = new DefineDialog(this);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Integer id){
        switch (id) {
            case R.id.dialog_min:
                moveTaskToBack(isFinishing());//返回键最小化应用
//                Intent intent=new Intent(Intent.ACTION_MAIN);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent.addCategory(Intent.CATEGORY_HOME);
//                startActivity(intent);
                break;
            case R.id.dialog_comfire:
                super.onBackPressed();
                break;
        }
    }

}
